<template>
<div>
   <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
     <el-form-item label="实验名称" >
       <el-input v-model="dataForm.experimentName" placeholder="实验名称" clearable style="width: 200px;"></el-input>
     </el-form-item>
	   <el-form-item label="老师名称">
	     <el-input v-model="dataForm.userName" placeholder="老师名称" clearable  style="width: 200px;"></el-input>
	   </el-form-item>
	   <el-form-item label="老师账号">
	     <el-input v-model="dataForm.account" placeholder="老师账号" clearable style="width: 200px;"></el-input>
	   </el-form-item>
     <el-form-item label="实验有效标记">
       <el-select v-model="dataForm.enableFlag" placeholder="请选择" style="width: 140px;" clearable>
           <el-option
             v-for="item in enableFlag"
             :key="item.value"
             :label="item.label"
             :value="item.value">
           </el-option>
         </el-select>
     </el-form-item>
      <el-form-item>
        <el-button type="primary"  plain size="mini" @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      :default-sort = "{order: 'descending'}"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        prop="id"
        type="index"
        fixed="left"
        header-align="center"
        align="center"
        width="80"
        label="ID">
      </el-table-column>
      <el-table-column
        prop="experimentName"
        header-align="center"
        width="180"
        fixed="left"
        align="center"
        label="实验名称">
        </el-table-column>
      <el-table-column
        prop="userName"
        header-align="center"
        align="center"
        label="教师名称">
      </el-table-column>
      <el-table-column
        prop="account"
        header-align="center"
        align="center"
        label="教师账号">
      </el-table-column
      <el-table-column
        prop="workFile"
        header-align="center"
        align="center"
        show-overflow-tooltip
        label="实验报告内容">
      </el-table-column>
      <el-table-column
        prop="experimentPlatform"
        header-align="center"
        align="center"
        label="实验操作平台">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="实验状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" size="small" type="danger">已关闭</el-tag>
          <el-tag v-else size="small">开启中</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="enableFlag"
        header-align="center"
        align="center"
        label="有效标志">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.enableFlag === 0" size="small" type="danger">失效</el-tag>
          <el-tag v-else size="small">有效</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="200px"
        label="操作">
        <template slot-scope="scope">
          <el-button  type="success"  plain size="mini" @click="ViewHandle(scope.row.uuid)">查看</el-button>
          <el-button  type="primary"  plain size="mini" @click="CensorHandle(scope.row.uuid,scope.row.experimentName,scope.row.userName,scope.row.account)">违规下线</el-button>
         </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[9, 18, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <subject-censor v-if="addOrUpdateVisible" ref="censorHandle"></subject-censor>
    <fun-view v-if="addOrUpdateVisible" ref="viewHandle"></fun-view>
  </div>
</template>

<script>
  /* eslint-disable */
  import SubjectCensor from './subject-censor.vue'
  import FunView from './fun-view.vue'


export default {
  components: {SubjectCensor,FunView},
    data () {
      return {
        dataForm: {
          userName: '',
          experimentName: '',
          account: '',
          enableFlag: ''
        },
        enableFlag:[{value: '0',label: '无效'}, {value: '1',label: '有效'}],
        dataList: [],
        pageIndex: 1,
        pageSize: 9,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    computed: {
      id: {
        get () { return this.$store.state.user.id },
        set (val) { this.$store.commit('user/updateId', val) }
      },
      account: {
        get () { return this.$store.state.user.account },
        set (val) { this.$store.commit('user/updateAccount', val) }
      },
      avatar: {
        get () { return this.$store.state.user.avatar },
        set (val) { this.$store.commit('user/updateAvatar', val) }
      }
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/adminCourse/getExperimentPageInfo'),
          method: 'post',
          data: this.$http.adornData({
            'experimentName': this.dataForm.experimentName,
            'account': this.dataForm.account,
            'userName': this.dataForm.userName,
            'enableFlag': this.dataForm.enableFlag,
            'pageNo': this.pageIndex,
            'pageSize': this.pageSize
          })
        }).then(({data}) => {
          if (data.success) {
            this.dataList = data.data.rows
            this.totalPage = data.data.rowTotal
          } else {
            this.dataList = []
            this.totalPage = 0
            this.$message.error(data.message)
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      CensorHandle (uuid,experimentName,userName,account){
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.censorHandle.init(uuid,experimentName,userName,account)
        })
      },
      ViewHandle (uuid){
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.viewHandle.init(uuid)
        })
      },
      // 删除
      deleteHandle (uuid) {
        var userIds = uuid ? [uuid] : this.dataListSelections.map(item => {
          return item.userId
        })
        this.$confirm(`确定要进行[${uuid ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/course/deleteExperimentInfo'),
            method: 'post',
            data: this.$http.adornData({
              'uuIds': userIds,
              'updateBy': this.$store.state.user.account
            })
          }).then(({data}) => {
            if (data.success) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.message)
            }
          })
        }).catch(() => {})
      }
    }
  }
</script>
<style lang="scss" scoped>
 .el-button--primary {
      color: #fff;
      background-color: #273471 !important;
      border-color: #273471 !important;
  }
</style>
